﻿using System.Linq;
using INHollandair.Core.Enum;
using INHollandair.Core.Models;
using INHollandair.Core.Repositories;

namespace INHollandair.Core.Services
{
    public class UserService
    {
        private readonly UserRepository _repo = new UserRepository();

        //User table queries
        public LoginEnum Login(string userName, string hash)
        {
            User user = _repo.UserRepo.FirstOrDefault(x => x.Name.Equals(userName) && x.Hash.Equals(hash));
            if (user != null)
            {
                return user.IsActivated? LoginEnum.Succes : LoginEnum.NotActivated;
            }
            return LoginEnum.NotFound;
        }
        public bool UserNameExists(string userName)
        {
            return _repo.UserRepo.Any(x => x.Name.Equals(userName));
        }
        public bool UserEmailExists(string email)
        {
            return _repo.UserRepo.Any(x => x.Email.Equals(email));
        }
        public bool UserEmailExistsExcludingUser(User user, string email)
        {
            return _repo.UserRepo.Any(x => x.Email.Equals(email) && !(x.Email.Equals(user.Email)));
        }
        public string GetUserSaltByName(string userName)
        {
            return _repo.UserRepo.Where(x => x.Name.Equals(userName)).Select(x => x.Salt).FirstOrDefault();
        }
        public User GetUserByEmailAddress(string address)
        {
            return _repo.UserRepo.FirstOrDefault(x => x.Email.Equals(address));
        }
        public User GetUserByName(string userName)
        {
            return _repo.UserRepo.FirstOrDefault(x => x.Name.Equals(userName));
        }
        public User GetUserById(int? id)
        {
            return _repo.UserRepo.FirstOrDefault(x => x.Id.Equals(id));
        }

        //Admin table queries
        public LoginEnum LoginAdmin(string userName, string hash)
        {
            User user = _repo.AdminRepo.FirstOrDefault(x => x.Name.Equals(userName) && x.Hash.Equals(hash));
            return user != null ? LoginEnum.Succes : LoginEnum.NotFound;
        }
        public string GetAdminSaltByName(string userName)
        {
            return _repo.AdminRepo.Where(x => x.Name.Equals(userName)).Select(x => x.Salt).FirstOrDefault();
        }

        //RegisterUrl table queries
        public RegisterURL GetRegisterUrlByUrl(string registerUrl)
        {
            return _repo.UserRegisterRepo.FirstOrDefault(x => x.RegisterUrl.Equals(registerUrl));
        }
        public RegisterURL GetRegisterUrlByUser(User user)
        {
            return _repo.UserRegisterRepo.FirstOrDefault(x => x.UserId.Equals(user.Id));
        }


        //ResetPasswordURL table queries
        public ResetPasswordURL GetResetPasswordUrlByUrl(string resetPasswordUrl)
        {
            return _repo.UserPassResetRepo.FirstOrDefault(x => x.ResetPasswordUrl.Equals(resetPasswordUrl));
        }
        public ResetPasswordURL GetResetPasswordUrlByUser(User user)
        {
            return _repo.UserPassResetRepo.FirstOrDefault(x => x.UserId.Equals(user.Id));
        }


        /// <summary>
        /// CRUD - implementatie data abstractie is niet compleet
        /// Services verwijzen nog wel door naar repo's
        /// </summary>
        public void InsertUser(User user)
        {
            _repo.InsertUser(user);
        }
        public void UpdateUser(User user)
        {
            _repo.UpdateUser(user);
        }
        public void RemoveUser(User user)
        {
            _repo.RemoveUser(user);
        }

        public void InsertRegisterUrl(RegisterURL registerUrl)
        {
            _repo.InsertRegisterUrl(registerUrl);
        }
        public void UpdateRegisterUrl(RegisterURL registerUrl)
        {
            _repo.UpdateRegisterUrl(registerUrl);
        }
        public void RemoveRegisterUrl(RegisterURL registerUrl)
        {
            _repo.RemoveRegisterUrl(registerUrl);
        }

        public void InsertResetPasswordUrl(ResetPasswordURL resetPasswordUrl)
        {
            _repo.InsertResetPasswordUrl(resetPasswordUrl);
        }
        public void UpdateResetPasswordUrl(ResetPasswordURL resetPasswordUrl)
        {
            _repo.UpdateResetPasswordUrl(resetPasswordUrl);
        }
        public void RemoveResetPasswordUrl(ResetPasswordURL resetPasswordUrl)
        {
            _repo.RemoveResetPasswordUrl(resetPasswordUrl);
        }
    }
}
